#include "TKEIndex.h"
#include "TKEScreen.h"
//#include <MAUI/EditBox.h>
#include <conprint.h>

#include "../std.h"
#include "../Widgets/Colours.h"
#include "../Widgets/UIBuilder.h"
#include "../Utilities/trim.h"

TKEIndex::TKEIndex(){

	mProcessor = new RANDProcessor();
	mProcessor2 = new STATProcessor();
	mainLayout = createMainLayout(getStringNotIterative(MAIN_TEXT_START + 3), getStringNotIterative(L_SELECT), " ", getStringNotIterative(L_BACK));

	listBox = (ListBox*) mainLayout->getChildren()[1];
	softKeys = (Widget*) mainLayout->getChildren()[2];

	titleCompany = createLabel(getStringNotIterative(L_SELECTCOMPANY), HEIGHTITEM);
	listBox->add(titleCompany);

	Label *label;
	label = createCenterLabel(getStringNotIterative(L_STATISTIC), HEIGHTITEM);
	listBox->add(label);

	label = NULL;

	/*label = createLabel(getStringNotIterative(L_MSG_INPUT), HEIGHTITEM*2);
	editbox = new EditBox(0, HEIGHTITEM, label->getWidth(), HEIGHTITEM, label, "", 0, APPCONTROLLER->getFont("default"), true, false, 2, EditBox::IM_NUMBERS);
	editbox->setDrawBackground(false);
	//editbox->setBackgroundColor(WHITESMOKE);
	label->addWidgetListener(this);
	listBox->add(label);*/

	label = createLabel(getStringNotIterative(L_MSG_INPUT), HEIGHTITEM);
	listBox->add(label);

	String mDefaultText = " ";
	WString mCaption = L"Enter here";
	editbox = new NativeEditBox(0, 0, APPCONTROLLER->getScreenWidth(), HEIGHTITEM , 128, MA_TB_TYPE_NUMERIC, mDefaultText, mCaption);
	editbox->setFont(APPCONTROLLER->getFont("default"));
	editbox->setCaption(" ");
	//editbox->setSkin(APPCONTROLLER->getSkin("default"));
	editbox->setDrawBackground(true);
	editbox->setBackgroundColor(DARKGOLDENROD);
	editbox->setHorizontalAlignment(Label::HA_LEFT);
	listBox->add(editbox);

	label = NULL;

	label = createCenterLabel(getStringNotIterative(L_ANALYSIS), HEIGHTITEM);
	listBox->add(label);

	this->setMain(mainLayout);
}

TKEIndex::~TKEIndex(){

}

/*void TKEIndex::selectionChanged(Widget* widget, bool selected){
	if (selected){
		widget->getChildren()[0]->setSelected(true);
	}
	else{
		widget->getChildren()[0]->setSelected(false);
	}
}*/

/*void TKEIndex::show(){
	//listBox->getChildren()[listBox->getSelectedIndex()]->setSelected(true);
	//Screen::show();
}*/

/*void TKEIndex::hide(){
	//listBox->getChildren()[listBox->getSelectedIndex()]->setSelected(false);
	//Screen::hide();
}*/

void TKEIndex::keyPressEvent(int keyCode, int nativeCode){
	switch(keyCode){
	case MAK_UP:
		listBox->selectPreviousItem();
		break;
	case MAK_DOWN:
		listBox->selectNextItem();
		break;
	case MAK_SOFTRIGHT:
		APPCONTROLLER->show("LCOMIndex");
		break;
	case MAK_SOFTLEFT:
	case MAK_FIRE:{
		int index = listBox->getSelectedIndex();
		Label* l = (Label*)softKeys->getChildren()[1];
		String url = APPCONTROLLER->getUrl();
		if (index == 0){
			APPCONTROLLER->show("LCOMIndex");
		}else if (index == 1){
			l->setCaption("Loading");
			l->update();
			mProcessor2->setInput(companyid);
			mProcessor2->addRequest(url, this);
		}else if (index == 3 || index == 2){
			editbox->activate();
		}else if (index == 4){
			l->setCaption("Loading");
			l->update();
			String temp = editbox->getCaption();
			if (temp.length()!=0){
				mProcessor->setInput(companyid, editbox->getCaption());
				mProcessor->addRequest(url, this);
				//APPCONTROLLER->show("TKEScreen");
			}else{
				listBox->setSelectedIndex(3);
				editbox->activate();
			}
		}
	}
		break;
	}
}

void TKEIndex::pointerPressEvent(MAPoint2d point){
	Point p;
	p.set(point.x, point.y);
	if (listBox->contains(p)) {
		for (int i = 0; i < listBox->getChildren().size(); i++) {
			if (listBox->getChildren()[i]->contains(p)) {
				int index = listBox->getSelectedIndex();
				if (index == i) keyPressEvent(MAK_SOFTLEFT, 0);
				else listBox->setSelectedIndex(i);
				break;
			}
		}
	} else if (softKeys->contains(p)) {
		if (softKeys->getChildren()[0]->contains(p)) {
			// Do nothing
		} else if (softKeys->getChildren()[1]->contains(p)) {
			keyPressEvent(MAK_SOFTRIGHT, 0);
		}
	}
}

void TKEIndex::pointerReleaseEvent(MAPoint2d point){

}

void TKEIndex::setCompany(String& id, const String& name){
	companyid = id;
	companyname = name;
	titleCompany->setCaption(name);
	titleCompany->update();
}

void TKEIndex::itemReceived(RANDItem& item){
	TKEScreen* screen= (TKEScreen*)APPCONTROLLER->getScreen("TKEScreen");

	LRESItem* newitem = new LRESItem();

	String temp = getStringIterative(L_DIGITS) + editbox->getCaption() + " - " + companyname;
	newitem->companyid = companyid;
	newitem->companyname = temp;

	//newitem->drawdate = widget->getDrawDate();
	//newitem->istoday = widget->getIsToday();
	newitem->outofdate = item.isood;
	newitem->result = item.result;
	//newitem->shortcode = widget->getShortCode();
	//newitem->smscnt = widget->getSmsCnt();

	screen->setTKEItem(newitem);

	delete newitem;

	APPCONTROLLER->show("TKEScreen");
}

void TKEIndex::processingComplete(){
	Label* l = (Label*)softKeys->getChildren()[1];
	l->setCaption(" ");
	l->update();
	lprintfln("process RAND complete");
}

void TKEIndex::processingstatComplete(){
	Label* l = (Label*)softKeys->getChildren()[1];
	l->setCaption(" ");
	l->update();
	lprintfln("process STAT complete");
}

void TKEIndex::itemstatReceived(STATItem& item){
	TKEScreen* screen= (TKEScreen*)APPCONTROLLER->getScreen("TKEScreen");

	LRESItem* newitem = new LRESItem();
	String temp = getStringIterative(L_STATISTIC) + " - " + companyname;

	newitem->companyid = companyid;
	newitem->companyname = temp;
	//newitem->drawdate = widget->getDrawDate();
	//newitem->istoday = widget->getIsToday();
	newitem->outofdate = item.isood;
	newitem->result = item.result;
	//newitem->shortcode = widget->getShortCode();
	//newitem->smscnt = widget->getSmsCnt();

	screen->setTKEItem(newitem);

	delete newitem;

	APPCONTROLLER->show("TKEScreen");
}

void TKEIndex::nativeEditFinished(){

}
